<template>
  <div>
    <div class="header">
      <!-- 搜索左边 -->
      <div class="category" @click="changeFlag">
        <i class="iconfont icon-fenlei"></i>
        <div class="cateText">分类</div>
      </div>
      <!-- 商标 -->
      <h1 @click="toHome">
        <img src="./image/decathlon-logo-chine.svg" class="logo" />
      </h1>
      <!-- 搜索 -->
      <div class="search">
        <!-- 输入框 -->
        <input type="text" placeholder="搜索商品或商品号"  :style="`width:${this.width}px`"/>
        <!-- 按钮 -->
        <button @click="toSearch">
          <i class="iconfont icon-sousuo"></i>
        </button>
      </div>
      <!-- 搜索右边 -->
      <div class="typeList">
        <div class="centerList">
          <i class="iconfont icon-help"></i>
          <div class="center">帮助</div>
        </div>

        <div class="centerList" @click="clear" v-if="$store.state.user.userInfo.name">
          <i class="iconfont icon-mendian"></i>
          <!-- <div class="center">登录</div> -->
          <div class="center" >退出</div>
        </div>
        <div class="centerList" @click="toLogin" v-else>
          <i class="iconfont icon-mendian"></i>
          <!-- <div class="center">登录</div> -->
           <div class="center" >登录</div>
        </div>

        <div class="centerList">
          <i class="iconfont icon-zhanghu"></i>
          <div class="center">账户</div>
        </div>

        <div
          class="centerList"
          style="background-color: #fdd935; width: 104px"
          @click="toShopCart"
        >
          <i class="iconfont icon-icon8"></i>
          <div class="center">购物车</div>
        </div>
      </div>
    </div>
    <Categorys v-if="this.flag"></Categorys>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import Categorys from "@/components/categorys";
export default {
  name: "Header",
  components: {
    Categorys,
  },
  data() {
    return {
      flag: false,
      screenWidth: document.body.clientWidth,
      width: "1083",
      screenWidth: "",
      
    };
  },
  mounted() {
    this.screenWidth = document.body.clientWidth;
    window.onresize = () => {
      return (() => {
        this.screenWidth = document.body.clientWidth;
        this.width = 1083-(1903-(this.screenWidth))
      })();
    };
  },
  methods: {
    // 去购物车
    toShopCart() {
      this.$router.push({ path: "/shopcart" });
    },
    // 去登陆
    toLogin() {
      this.$router.push({ path: "/login" });
    },
    async clear(){
      try {
        await this.$store.dispatch('userLogout')
        alert('用户退出成功')
        this.$router.push('/home')
      } catch (error) {
        alert('用户退出失败')
      }      
    },
    // 去首页
    toHome() {
      this.$router.push({ path: "/home" });
    },
    // 去搜索页面
    toSearch() {
      this.$router.push({ path: "/search" });
    },
    // 获取页面视口大小
    // getInnerWidth(){
    //   this.width = window.innerWidth + 'px';
    //   console.log(this.width)
    // },

    // 三级列表显示
    changeFlag() {
      this.flag = !this.flag;
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  font-size: 14px;
  width: 100%;
  height: 53px;
  box-shadow: 2px 2px 4px 0 rgb(0 83 125 / 10%);
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  z-index: 999;
  position: fixed;
  top: 0;
  left: 0;
  overflow: hidden;
  // 三级列表分类样式
  .category {
    display: flex;
    width: 91px;
    height: 100%;
    background-color: #edeff1;
    border-left: 2px solid #0082c3;
    cursor: pointer;
    .iconfont {
      font-size: 20px;
      line-height: 53px;
      margin: 0 10px 0 18px;
      color: #575859;
    }
    .cateText {
      color: #575859;
      font-size: 12px;
      font-weight: 700;
      line-height: 53px;
    }
  }
  // logo样式
  .logo {
    width: 211px;
    margin: 11px 0 0 10px;
    cursor: pointer;
  }
  // 搜索样式
  .search {
    height: 100%;
    line-height: 53px;
    margin: 0 14px 0 20px;
    input {
      border: none;
      height: 35px;
      width: 1083px;
      background-color: #edeff1;
      padding-left: 9px;
      font-size: 14px;
      font-weight: 700;
      color: #0082c3;
      &:focus {
        outline: none;
      }
    }
    button {
      border: none;
      width: 59px;
      height: 34px;
      background-color: white;
      border: 2px solid #edeff1;
      cursor: pointer;
      .iconfont {
        font-size: 18px;
        color: #0082c3;
      }
    }
  }
  // 购物车样式
  .typeList {
    display: flex;
    // justify-content:flex-end;
    .centerList {
      border-left: 2px solid #edeff1;
      display: flex;
      width: 86px;
      cursor: pointer;
      .iconfont {
        line-height: 53px;
        margin: 0 6px 0 14px;
      }
      .center {
        line-height: 53px;
      }
    }
  }
}
</style>
